let app = {
  pageHelper: {
    global_page: 1,
    global_limit: 10
  },
  urlLocation: {
    studentUrl: '/student',
    queryAllStu: '/student/queryAll',
    queryByNo: '/student/queryByNo',
    addStudent: '/student/addStudent',
    updateStudent: '/student/updateStudent',
    deleteStudent: '/student/deleteStudent',
    queryAllTeacher: '/teacher/queryAll',
    queryTeacherById: '/teacher/queryById',
    queryTeaByNo: '/teacher/queryByNo',
    queryTeacherByCondition: '/teacher/queryByCondition',
    addTeacher: '/teacher/addTeacher',
    updateTeacher: '/teacher/updateTeacher',
    deleteTeacher: '/teacher/deleteTeacher',
    queryAllCt: '/clazz/queryAll',
    queryCtByName: '/clazz/queryByName',
    addCt: '/clazz/addClazzType',
    deleteCt: '/clazz/deleteClazzType',
    queryAllTest: '/test/queryAll',
    addCusTestPaper: '/test/addCusTest',
    queryAllClazz: '/test/queryAllClazz',
    addRandomTest: '/test/addRandomTest',
    queryStuByNoAndPwd: '/student/queryByNoAndPwd',
    queryTestPaperIsDone: '/test/queryTestPaperIsDone',
    querySubjectByTestId: '/test/querySubjectByTestId',
    addAp: '/answer/addAp',
    queryApByTestId: '/answer/queryApByTestId',
    queryTpDetail: '/answer/queryTestPaperDetail'
  },
  initPage: () => {
    $('.layui-header').load('/admin/public/header.html');
    $('.layui-side').load('/admin/public/menu.html');
  },
  regexArr: {
    regexFunction: {
      regEmail: email => {
        return /^([A-Za-z0-9_\-.])+@([A-Za-z0-9_\-.])+\.([A-Za-z]{2,4})$/.test(email);
      },
      regPhone: phone => {
        return /^1[3456789][0-9]{9}$/.test(phone);
      },
      regNumber: number => {
        return /^\d+$/.test(number);
      },
      regAge: age => {
        return /^[0-9]{1,2}$/.test(age);
      }
    }
  },
  formatDate: date => {
    let y = date.getFullYear(),
      M = date.getMonth() + 1,
      d = date.getDate(),
      H = date.getHours(),
      m = date.getMinutes(),
      s = date.getSeconds();
    return y + '-' + (M < 10 ? ('0' + M) : M) + '-' + (d < 10 ? ('0' + d) : d) + " " +
      (H < 10 ? ('0' + H) : H) + ":" + (m < 10 ? ('0' + m) : m) + ":" + (s < 10 ? ('0' + s) : s);
  },
  resetForm: () => {
    layui.use('form', function () {
      let form = layui.form;
      form.render();
    });
  },
  alertInfo: msg => {
    layui.use('layer', () => {
      layui.layer.msg(msg)
    })
  }
};

function isStuLogin() {
  $.get(`/student/isLogin`, res => {
    if (res === '') {
      // console.info('account is not login');
      window.location.href = '/index';
    } else {
      $('.layui-nav img').attr('alt', res.id);
      $('#name').text(res.name);
      localStorage.setItem('stuId', res.id);
    }
  })
}

function isTeaLogin() {
  $.get(`/teacher/isLogin`, res => {
    if (res === '') {
      // console.info('teacher has not login');
      window.location.href = '/teacher';
    } else {
      localStorage.setItem('teaId', res.id);
      localStorage.setItem('clazz', res.clazz);
      $('.layui-nav img').attr('alt', res.id);
      $('#name').text(res.name);
    }
  })
}

function showLayout(elem) {
  let timeout = 500;
  elem.find('.layout-background').removeClass('layui-hide').removeClass('background-hide', timeout);
  elem.find('.layout-box').removeClass('layui-hide').removeClass('box-hide', timeout);
}

function hideLayout(elem) {
  let timeout = 500;
  elem.find('.layout-background').addClass('background-hide', timeout);
  elem.find('.layout-box').addClass('box-hide', timeout);
  setTimeout(() => {
    elem.find('.layout-background, .layout-box').addClass('layui-hide');
    elem.find('form')[0].reset();
  }, timeout);
}